import 'package:flutter/services.dart';

import 'channel_error_handler.dart';

/// 使用EventChannel实现，专门处理iOS端持续发送的数据流
class DataEventChannel {
  static const EventChannel _eventChannel = EventChannel('data_event_channel');

  /// 获取iOS数据流Stream
  /// 用于监听iOS端持续发送的数据
  static Stream<Map<String, dynamic>> get nativeDataStream => _eventChannel
      .receiveBroadcastStream()
      .map((event) {
        try {
          return Map<String, dynamic>.from(event);
        } on PlatformException catch (e) {
          ChannelErrorHandler.handleError(e, '数据转换失败', 'DataEventChannel');
          return <String, dynamic>{};
        }
      })
      .handleError((error) {
        ChannelErrorHandler.handleError(error, 'Stream错误', 'DataEventChannel');
      });
}
